package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.*;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.vo.ExportProductResult;
import cn.itcast.vo.ExportResult;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.List;
import java.util.UUID;

@Service
public class ExportServiceImpl implements ExportService {

	@Autowired
	private ExportDao exportDao;

	@Autowired
	private ContractDao contractDao;

	@Autowired
	private ContractProductDao contractProductDao; //合同货物dao

	@Autowired
	private ExportProductDao exportProductDao; //报运单商品dao

	@Autowired
	private ExtCproductDao extCproductDao;//合同附件dao

	@Autowired
	private ExtEproductDao extEproductDao;//报运单附件dao

	@Override
	public Export findById(String id) {
		return exportDao.selectByPrimaryKey(id);
	}


	/**
	 * 新增报运单
	 *      报运单对象：
	 *          contractIds：合同id集合，从页面传递过来
	 *          customerContract：合同号集合
	 */
	public void save(Export export) {
		//i.根据合同创建报运单
		//1.设置报运单id
		export.setId(UUID.randomUUID().toString());
		//2.获取合同id数组
		String contractIds[] = export.getContractIds().split(",");
		//3.查询所有的合同
		//4.根据合同构造合同号的字符串，以“ ”隔开
		String customerContract ="";
		int proNum = 0;
		int extNum = 0;
		for (String contractId : contractIds) {
			Contract contract = contractDao.selectByPrimaryKey(contractId);
			customerContract += contract.getContractNo() +" ";//合同号
			//修改合同状态 0：草稿，1：以提交（上报） 2：已保运
			contract.setState(2);
			contractDao.updateByPrimaryKeySelective(contract);
			proNum += contract.getProNum();
			extNum += contract.getExtNum();
		}
		//5.设置报运单的合同号集合
		export.setCustomerContract(customerContract);
		//ii.根据合同货物创建报运单商品
		//1.查询购销合同下的所有货物
		ContractProductExample epExample = new ContractProductExample();
		ContractProductExample.Criteria epCriteria = epExample.createCriteria();
		epCriteria.andContractIdIn(Arrays.asList(contractIds)); //将数组转化为list集合
		List<ContractProduct> cps = contractProductDao.selectByExample(epExample);
		//2.循环购销合同货物，创建报运单商品
		for (ContractProduct cp : cps) {
			ExportProduct ep = new ExportProduct();
			//3.普通属性，设置商品id，所属的报运单id
			BeanUtils.copyProperties(cp,ep);//对象属性copy （将cp中的同名属性值设置到ep对象中）
			ep.setId(UUID.randomUUID().toString());
			ep.setExportId(export.getId()); //报运单id
			//4.保存报运单商品
			exportProductDao.insertSelective(ep);

			//iii.根据合同附件创建报运单附件
			//1.根据货物id，查询货物的所有附件
			ExtCproductExample ecExample = new ExtCproductExample();
			ExtCproductExample.Criteria ecCriteria = ecExample.createCriteria();
			ecCriteria.andContractProductIdEqualTo(cp.getId());
			List<ExtCproduct> ecps = extCproductDao.selectByExample(ecExample);
			//2.循环货物附件，构造报运单附件
			for (ExtCproduct ecp : ecps) {  //合同附件
				ExtEproduct eep = new ExtEproduct(); //报运单附件
				//3.设置报运单附件信息，报运单附件id，报运单商品id
				BeanUtils.copyProperties(ecp,eep);
				eep.setId(UUID.randomUUID().toString());
				eep.setExportProductId(ep.getId());
				eep.setExportId(export.getId());
				//4.保存报运单附件
				extEproductDao.insertSelective(eep);
			}
		}

		//保存报运单
		//设置报运单附件数和货物数
		export.setProNum(proNum); //货物数
		export.setExtNum(extNum); //附件
		//设置报运单状态
		export.setState(0);
		exportDao.insertSelective(export);
	}

	/**
	 * 更新报运单同时更新商品
	 *  参数：
	 *      报运单：export
	 *      报运单中包含了所有需要更新的商品：exportProducts
	 */
	public void update(Export export) {
		//更新报运单
		exportDao.updateByPrimaryKeySelective(export);
		//更新商品
		if (export.getExportProducts() != null && export.getExportProducts().size()>0) {
			for (ExportProduct ep :export.getExportProducts() ) {
				exportProductDao.updateByPrimaryKeySelective(ep);
			}
		}
	}

	@Override
	public void delete(String id) {

	}

	@Override
	public PageInfo findAll(ExportExample example, int page, int size) {
		PageHelper.startPage(page,size);
		List<Export> list = exportDao.selectByExample(example);
		return new PageInfo(list);
	}

	//更新电子保运结果

	/**
	 * 参数 ：ExportResult
	 *          报运单结果
	 *             所有商品set集合
	 */
	public void updateE(ExportResult exportResult) {
		//1.更新报运单
		Export export = exportDao.selectByPrimaryKey(exportResult.getExportId());
//		String contractIds = export.getContractIds();
//		String ids [] = contractIds.split(",");
//		for (String id : ids) {
//			Contract contract = contractDao.selectByPrimaryKey(id);
//			contract.setState(3);
//		}
		export.setState(exportResult.getState());
		export.setRemark(exportResult.getRemark());
		exportDao.updateByPrimaryKeySelective(export);
		//2.更新所有商品
		if (exportResult.getProducts() != null && exportResult.getProducts().size()>0) {
			for (ExportProductResult epr :exportResult.getProducts() ) {
				ExportProduct product = exportProductDao.selectByPrimaryKey(epr.getExportProductId());
				product.setTax(epr.getTax());
				exportProductDao.updateByPrimaryKeySelective(product);
			}
		}
	}
}
